#Author http://www.cnblogs.com/Jame-mei
from db import db_handler
import os






def get_host_dic_interface(host):
    '''
    1.通过主机名得到主机的详细信息
    :param host:
    :return:
    '''
    flag,msg=db_handler.get_hosts_info(host)
    if flag:
        return flag,msg
    else:
        return flag,msg






def common_batch_interface(cmd):
    '''
    2.拆分命令-h -g -cmd/ -action -local -remote 等操作
    :param cmd:
    :return:
    '''
    host_dic={'-h':None,'-g':None}
    run_dic={'-cmd':None}
    scp_dic={'-action':None,'-local':None,'-remote':None}
    allhost=[]

    if not '-h' in cmd or  not '-g' in cmd:
        return False,'-h 或 -g 必须指定.'
    else:

        h_index=cmd.split(' ').index('-h')+1
        host_dic['-h']=cmd.split(' ')[h_index]

        host_list1=host_dic['-h'].split(',')
        print('-h参数后面',host_list1)
        if not host_dic['-h']:
            return False,'注意:-h 后面要跟主机名哦'



        g_index=cmd.split(' ').index('-g')+1
        host_dic['-g']=cmd.split(' ')[g_index]
        if not host_dic['-g']:
            return False,'注意:-g 后面要跟组名哦'

        flag,host_list2=db_handler.get_groups_info(host_dic['-g'])
        print('-g 参数后面:', host_list2)
        if not flag:
            return False,host_list2

    allhost=set(host_list1+host_list2) #去重复




    if '-cmd' in cmd:
        flag,msg=batch_run_interface(run_dic,cmd)
        if flag:
            return dict(host_dic,**run_dic),allhost #返回字典，所有主机列表
        else:
            return False,msg


    if  '-action' in cmd:
        flag,msg=batch_scp_interface(scp_dic,cmd)
        if flag:
            return dict(host_dic,**scp_dic),allhost
        else:
            return False,msg










def batch_run_interface(run_dic,cmd):
    '''
    2.解析执行远程命令paramiko接口模块

    :param cmd:
    :return:
    '''
    cmd_index=cmd.split('-cmd')[1].strip()
    run_dic['-cmd']=cmd_index.strip('"')
    if not run_dic['-cmd']:
        return False,'注意:-cmd 后面不能为空.'

    return True,run_dic






def batch_scp_interface(scp_dic,cmd):
    '''
    3.进行远程下载、上传功能接口模块

    :param cmd:
    :return:
    '''
    action_index=cmd.split(' ').index('-action')+1
    scp_dic['-action']=cmd.split(' ')[action_index]

    if not scp_dic['-action']:
        return False,'注意:-action 后面要有执行的命令.'
    else:
        if '-local' in cmd and '-remote' in cmd:
            local_index=cmd.split(' ').index('-local')+1
            remote_index=cmd.split(' ').index('-remote')+1
            scp_dic['-local']=cmd.split(' ')[local_index]


            scp_dic['-remote']=cmd.split(' ')[remote_index]

        else:
            return False,'-local 或 -remote 不能省略.'

        if scp_dic['-action']=='put':
            if not os.path.exists(scp_dic['-local']):
                return False,'%s 不存在，请确认后本地文件是否存在！'%scp_dic['-local']
            scp_dic['-action']='put'


        elif scp_dic['-action']=='get':
            scp_dic['-action']='get'

        else:
            return False,'注意:-action 后面的执行命令只能为put/get...'

        print('scp后面的字典======>:',scp_dic)
        return True,scp_dic



